package lzit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import lzit.entity.Shanghus;
import lzit.util.DBUtil;

public class ShanghusDao {

    public int add(Shanghus m) {
        Connection con = null;
        PreparedStatement stmt = null;
        int row = 0;
        try {
            con = DBUtil.getCon();
            String sql = "INSERT INTO Shanghus(sh_name,sh_quyu,sh_lianxiren,sh_dianhua) VALUES(?,?,?,?)";
            stmt = con.prepareStatement(sql);
            stmt.setString(1, m.getSh_name());
            stmt.setString(2, m.getSh_quyu());
            stmt.setString(3, m.getSh_lianxiren());
            stmt.setString(4, m.getSh_dianhua());
            row = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("添加商户信息失败: " + e.getMessage());
        } finally {
            DBUtil.release(null, stmt, con);
        }
        return row;
    }

    public Shanghus findShById(int id) {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Shanghus shanghu = null;
        try {
            con = DBUtil.getCon();
            String sql = "SELECT * FROM SHANGHUS WHERE id=?";
            stmt = con.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                shanghu = new Shanghus();
                shanghu.setId(rs.getInt("id"));
                shanghu.setSh_name(rs.getString("sh_name"));
                shanghu.setSh_quyu(rs.getString("sh_quyu"));
                shanghu.setSh_lianxiren(rs.getString("sh_lianxiren"));
                shanghu.setSh_dianhua(rs.getString("sh_dianhua"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询商户信息失败: " + e.getMessage());
        } finally {
            DBUtil.release(rs, stmt, con);
        }
        return shanghu;
    }

    public int update(Shanghus m) {
        Connection con = null;
        PreparedStatement stmt = null;
        int row = 0;
        try {
            con = DBUtil.getCon();
            String sql = "UPDATE Shanghus SET sh_name=?,sh_quyu=?,sh_lianxiren=?,sh_dianhua=? WHERE id=?";
            stmt = con.prepareStatement(sql);
            stmt.setString(1, m.getSh_name());
            stmt.setString(2, m.getSh_quyu());
            stmt.setString(3, m.getSh_lianxiren());
            stmt.setString(4, m.getSh_dianhua());
            stmt.setInt(5, m.getId());
            row = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("更新商户信息失败: " + e.getMessage());
        } finally {
            DBUtil.release(null, stmt, con);
        }
        return row;
    }

    public int delete(int id) {
        Connection con = null;
        PreparedStatement stmt = null;
        int row = 0;
        try {
            con = DBUtil.getCon();
            String sql = "DELETE FROM SHANGHUS WHERE id=?";
            stmt = con.prepareStatement(sql);
            stmt.setInt(1, id);
            row = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("删除商户信息失败: " + e.getMessage());
        } finally {
            DBUtil.release(null, stmt, con);
        }
        return row;
    }

    public List<Shanghus> findAllSh(String sql) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Shanghus> list = new ArrayList<Shanghus>();
        try {
            con = DBUtil.getCon();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Shanghus shanghus = new Shanghus();
                shanghus.setId(rs.getInt("id"));
                shanghus.setSh_name(rs.getString("sh_name"));
                shanghus.setSh_quyu(rs.getString("sh_quyu"));
                shanghus.setSh_lianxiren(rs.getString("sh_lianxiren"));
                shanghus.setSh_dianhua(rs.getString("sh_dianhua"));
                list.add(shanghus);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询所有商户信息失败: " + e.getMessage());
        } finally {
            DBUtil.release(rs, stmt, con);
        }
        return list;
    }
}